const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { VueLoaderPlugin } = require('vue-loader');
const webpack = require('webpack');
const readEnv = require('../src/utils/readEnv.js');
const env = readEnv('../../.env.dev');

module.exports = {
  entry: {
    app: path.resolve(__dirname, '../src/main.js')
  },
  output: {
    path: path.resolve(__dirname, '../dist'),
    filename: 'shop4v.js',
    clean: true
  },
	mode: 'development',
  module: {
    rules: [
      {
        test: /\.js$/,
        loader: "babel-loader",
        exclude: /node_modules/
      },
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader']
      },
      {
				test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
				loader: 'url-loader',
				options: {
					limit: 10000,
					outputPath: 'imag/',
					publicPath: '/imag/',
					esModule: false
				}
			},
			{
				test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
				loader: 'url-loader',
				options: {
					limit: 10000
				}
			},
      { test: /\.scss$/, use: ['style-loader', 'css-loader', 'sass-loader',] },
			// 加载VUE
			{
				test: /\.vue$/,
				loader: 'vue-loader'
			},
			{
				test: /.less$/,
				use: [
					'style-loader',
					'css-loader',
					'less-loader'
				]
			}
    ]
  },
  resolve: {
    extensions: ['.js', '.vue', '.json'],
    alias: {
      'vue$': 'vue/dist/vue.esm.js',
			'@': resolve('/src')
    }
  },
  plugins: [
    // 复制index.html
		new HtmlWebpackPlugin({
			title: "shop4v",
			template: "index.html"
		}),
		// 加载VUE
		new VueLoaderPlugin(),
    new webpack.DefinePlugin({ // 定义环境和变量
      'process.env': {
        ENV_CONFIG: env,
      }
    })
  ],
  devServer: {
    static: path.resolve(__dirname, 'static'),
    open: {
      target: ['/'],
      app: {
        name: 'chrome',
      }
    },
    host: process.env.HOST || 'localhost' || '0.0.0.0',
    hot: true,
    port: 8887,
  },
  devtool: "inline-source-map",
}

/**
 * 解析路径
 */
 function resolve(dir) {
	return path.join(__dirname, dir);
}